loop: ; this is a labelNOTE: Labels cannotbe indented!
ld a,5
push af ;just somestuff
push bc
inc a
anotherlabel: ; this is another label
JR
The ASM code is
always processed sequently, oneinstruction after the next. However, if you
want to skip to a certainpart of the code, or go back and repeat a previous
portion you must jumpthere. There are 2 different ways to JUMP to another
part of code. JR, and JP. There are a few differences that are
discussed later,but
they are VERY IMPORTANT!
To jump to a certain
part of code you must enterJR LABELNAME. Here are some
examples:
ld a,1 ;a=1NOTE: This code willcause an infinite loop. The code between 'MyLabel' and 'jr MyLabel'will be repeated over and over.
MyLabel: ;LABEL
inc a ;a=a+1
jr MyLabel ;jump to LABEL
In order to make jumps ONLY IF certain requirmentsare met, we use the following conditionals:
ld a,5 ;a=5This loop will execute 5 times, it doesn't do anythingyet, but in a few lessons, we'll get it to do somthing cool, don't worry:)
Loop: ;MyLOOP Label
dec a ;a=a-1
jr nz,Loop ;if A is not zero then jump toLoop.
;(When the value of A changes F is ALWAYS updated)
back
to list of instructions
JP
The ASM code is always processed
sequently, oneinstruction after the next. However, if you want to skip to
a certainpart of the code, or back to a previous portion you must jump
there. There are 2 different ways to JUMP to another part of code.
JR, andJP. There are a few differences that are discussed later, but
theyare VERY IMPORTANT!
To jump to a certain part
of code you must enterJP LABELNAME. Here are some examples:
ld a,1 ;a=1NOTE: This code willcause an infinite loop. The code between 'MyLabel' and 'jr MyLabel'will be repeated over and over.
MyLabel: ;LABEL
inc a ;a=a+1
jp MyLabel ;jump to LABEL
In order to make jumps ONLY IF certain requirmentsare met, we use the following conditionals:
ld a,5 ;a=5This loop will execute 6 times (a=5,4,3,2,1,0), itdoesn't do anything yet, but in a few lessons, we'll get it to do somthingcool, don't worry :)
Loop: ;MyLOOP Label
dec a ;a=a-1
jp p,Loop ;if A is positive then jumpto Loop.
;(When the value of A changes F is ALWAYS updated)
DIFFERENCES BETWEEN JR/JP
The Main difference bettween the 2 types of jumps(jr, jp) is the
JR is a RELETIVE jump. When translatedinto machine code it
essential says 'jump this many bytes forward/backward',for this reason, it has a
limited range. If you plan on jumping furtherthen the range allowed by JR
you MUST use JP. JP isan ABSOLUTE jump. When translated
into machine code it basicallysays 'jump to this location'. The advantage
of JR over JP is bothsize and speed in the final compiled
program.
Another difference, which we have
already seenis that JP allows you to use more conditions. JR does not
supportP,M,PO, or PE. I have not covered all these conditions, but I
willin Future lessons/examples.
DJNZ
DJNZ is a very
helpful instruction. It combinesthe [DEC -> JP nz,label] sequence used
so often in loops.
ld b,15 ;b=15, the number of times for the loop to executeNOTE: DJNZ always usesthe register B. It decrements B, checks if Bis nz, if so then jumps to the label.
Loop:
; all the cool loop stuff inside of here
djnz Loop ;b=b-1, jump isb is NotZero
ld a,0back to list of instructions
ld b,15
Loop:
push bc ;adds it to the stack
ld b,4 ;b=4
add a,b ;a=a+b, a=a+4
pop bc ;gets our value of Bback!
djnz Loop